package cn.jy.map.service.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.jy.map.domain.Communrecord;
import cn.jy.map.domain.Gjzxx;
import cn.jy.map.mapper.MapNewMapper;
import cn.jy.map.service.MapNewService;
import cn.jy.map.utils.MapUtils;

@Service("mapNewService")
public class MapNewServiceImpl implements MapNewService {

	@Autowired
	MapNewMapper mapNewMapper;
	
	Logger logger = Logger.getLogger(MapNewServiceImpl.class);
	
	@Override
	public List<Gjzxx> getGjzxx(String code) {
		logger.info("根据条件【"+code+"】模糊查询浆员卡号、姓名、身份证号满足条件的浆员信息!");
		
		return mapNewMapper.getGjzxx(code);
	}

	@Override
	public void updateLcRetComContent() {
		logger.info("更新每个浆员轮次表中的最新回访、提醒记录的沟通内容!");
		
		//获取所有有回访记录的浆员
		List<String> pList = mapNewMapper.getCommProvider();
		
		//查询每个浆员的最新的回访记录
		if(pList != null){
			for (String providerNo : pList) {
				List<Communrecord> list = mapNewMapper.getCommunrecordByPNo(providerNo, "1");
				if(list != null && list.size()>0){
					//更新轮次表中的最新沟通记录
					Communrecord comm = list.get(0);
					String content = "";
					if(comm.getGtnr() != null && !"".equals(comm.getGtnr())){
						content = comm.getGtnr();
						//2018-04-23[<p>电话不通，给朋友带话</p>
						content = content.replaceAll("<p>", "");
						content = content.replaceAll("</p>", "");
						content = content.trim();
						content = comm.getScsj().substring(0, 10)+"["+content+"]";
					}else{
						content = comm.getScsj().substring(0, 10);
					}
					mapNewMapper.updateLcCommContent(providerNo, content);
					logger.info("更新浆员【"+providerNo+":"+comm.getProviderName()+"】轮次表中的最新回访、提醒记录的沟通内容为【"+content+"】成功!!");
				}
				
			}
		}
		
		
		
	}

	@Override
	public List<Communrecord> getCommunrecord(String providerNo) {
		logger.info("获取浆员【"+providerNo+"】的沟通记录!");
		return mapNewMapper.getCommunrecordByPNo(providerNo, null);
	}

	@Override
	public List<Communrecord> getRemind(String providerNo) {
		logger.info("获取浆员【"+providerNo+"】的提醒记录!");
		return mapNewMapper.getRemindByPNo(providerNo);
	}

	@Override
	public void setGjzJwdIsDiscarded() {
		logger.info("更新供浆者地址经纬度表的所有浆员，将废弃浆员的isDiscarded 是否已废弃字段设置为 1 ，表示已经废弃");
		String providerNos = MapUtils.allForgeProviderNoStr;
		
		mapNewMapper.setGjzJwdIsDiscarded(providerNos);
		
		
	}

}
